package org.Keks.graphics;

import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.Keks.graphics.KeksBitmap;

/**
 * The Sprites class is a collection for all spritesheets which are used in a game.
 * If you want to use a sprite you have to create a Sprites object which then returns a Bitmap (the actual representation) based on the method which was used on the spritesheet.
 * @author kevin
 *
 */
public class KeksSprite {
	/**
	 * Cuts a part from a Bitmap beginning at 0/0 (X/Y) pixel of the picture.
	 * @param string Location of the bitmap.
	 * @param w Width of the desired new Bitmap.
	 * @param h Height of the desired new Bitmap.
	 *
	 */
	public static KeksBitmap[][] cut(String string, int w, int h) {
		return cut(string, w, h, 0, 0);
	}

	/**
	 * Cuts a part from a Bitmap.
	 * @param string Location of the bitmap.
	 * @param w Width of the desired new Bitmap.
	 * @param h Height of the desired new Bitmap.
	 * @param bx X position to start from.
	 * @param by Y position to start from.
	 *
	 */
	public static KeksBitmap[][] cut(String string, int w, int h, int bx, int by) {
		try {
			BufferedImage bi = ImageIO.read(new File(string));

			int xTiles = (bi.getWidth() - bx) / w;
			int yTiles = (bi.getHeight() - by) / h;

			KeksBitmap[][] result = new KeksBitmap[xTiles][yTiles];

			for (int x = 0; x < xTiles; x++) {
				for (int y = 0; y < yTiles; y++) {
					result[x][y] = new KeksBitmap(w, h);
					bi.getRGB(bx + x * w, by + y * h, w, h, result[x][y].pixels, 0, w);
				}
			}
			return result;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Get colors from Bitmap[][].
	 * @param bitmap Bitmap[][] you wish to check.
	 * @return Returns int[][] with colors.
	 */
	public static int[][] getColors(KeksBitmap[][] bitmap) {
		int[][] result = new int[bitmap.length][bitmap[0].length];
		for (int y = 0; y < bitmap[0].length; y++) {
			for (int x = 0; x < bitmap.length; x++) {
				result[x][y] = getColor(bitmap[x][y]);
			}
		}
		return result;
	}

	/**
	 * Receive color from a bitmap.
	 * @param bitmap
	 * @return Returns int[] with colors.
	 */
	public static int getColor(KeksBitmap bitmap) {
		int r = 0;
		int g = 0;
		int b = 0;
		for (int i = 0; i < bitmap.pixels.length; i++) {
			int col = bitmap.pixels[i];
			r += (col >> 16) & 0xff;
			g += (col >> 8) & 0xff;
			b += (col) & 0xff;
		}

		r /= bitmap.pixels.length;
		g /= bitmap.pixels.length;
		b /= bitmap.pixels.length;

		return 0xff000000 | r << 16 | g << 8 | b;
	}

	/**
	 * Loads a Bitmap.
	 * @param string Location of the bitmap.
	 *
	 */
	public static KeksBitmap load(String string) {
		try {
			BufferedImage bi = ImageIO.read(new File(string));
			
			int w = bi.getWidth();
			int h = bi.getHeight();

			KeksBitmap result = new KeksBitmap(w, h);
			bi.getRGB(0, 0, w, h, result.pixels, 0, w);

			return result;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * Creates a bitmap array from a sprite sheet that has only one row e.g. animations
	 * @param string Path of the file.
	 * @param h Height of the sprite.
	 * @return
	 */
	public static KeksBitmap[] cut(String string, int h) {
		try {
			BufferedImage bi = ImageIO.read(new File(string));
			
			int yTiles = bi.getHeight() / h;
			int w = bi.getWidth();

			KeksBitmap[] result = new KeksBitmap[yTiles];

			for (int y = 0; y < yTiles; y++) {
				result[y] = new KeksBitmap(w, h);
				bi.getRGB(0, y * h, w, h, result[y].pixels, 0, w);
			}
			return result;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
}